<template>
  <div class="app-car-container">
    <CustomContent>
      <template v-slot:body>
        <MmvTitle title="客户标签"></MmvTitle>
        <el-row>
          <el-col :sm="24" :md="18" :lg="16" class="table-padding">
            <el-collapse
              accordion
              style="
                border: 1px solid rgb(242 242 242);
                text-align: left;
                border-radius: 4px;
                background-color: rgb(242 242 242);
              "
            >
              <el-row style="padding: 10px 20px; box-sizing: boder-box">
                <el-col :span="24" style="font-size: 18px">
                  <div
                    style="
                      color: rgb(51 51 51);
                      margin: 10px 10px;
                      letter-space: 2px;
                    "
                  >
                    <i class="el-icon-info"></i> 客户标签指南：
                  </div>
                </el-col>
                <el-col :span="24">
                  <div
                    style="
                      color: rgb(163 155 155);
                      line-height: 2em;
                      text-indent: 4em;
                      letter-space: 2px;
                    "
                  >
                    客户标签可根据自定义规则来区分客户群体，老客户的标签类型可能会存在数据遗漏，系统会将遗漏客户标签设置为“老客户”
                  </div>
                  <div
                    style="
                      color: rgb(66 63 63);
                      line-height: 2em;
                      text-indent: 4em;
                      letter-space: 2px;
                    "
                  >
                    客户标签设置关系到客户营销创建活动模块，请慎重填写自定义规则，也可直接应用使用系统默认数据。
                  </div>
                </el-col>
              </el-row>
            </el-collapse>
          </el-col>
          <el-col :sm="24" :md="18" :lg="16" class="table-padding">
            <div v-for="(data, index) in labelList" :key="index">
              <el-row :gutter="16" v-if="data.labelType == 1">
                <el-form size="medium" label-width="0px">
                  <el-col :span="4">
                    <el-form-item label="">
                      <div
                        style="
                          border: 1px solid rgb(106 100 100);
                          text-align: center;
                          border-radius: 4px;
                        "
                      >
                        <dict-tag
                          :options="dict.type.label_classify"
                          :value="data.labelClassify"
                        />
                      </div>
                    </el-form-item>
                  </el-col>
                  <div v-if="data.labelPattern == 1">
                    <el-col :span="6">
                      <el-form-item>
                        <label style="margin-right: 10px; color: #6c6b6b"
                          >下单不超过</label
                        >
                        <el-input
                          v-model="data.orderSum"
                          :style="{ width: '30%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label style="margin-left: 10px; color: #6c6b6b"
                          >次</label
                        >
                      </el-form-item>
                    </el-col>
                  </div>
                  <div
                    v-if="data.labelPattern == 2"
                    class="grid-content bg-purple-light"
                  >
                    <el-col :span="12">
                      <el-form-item>
                        <label style="margin-right: 10px; color: #6c6b6b"
                          >订单金额超过</label
                        >
                        <el-input
                          v-model="data.amountSum"
                          :style="{ width: '15%' }"
                          @keyup.native="keyupEvent($event, input)"
                        >
                        </el-input>
                        <label
                          style="
                            margin-left: 10px;
                            margin-right: 10px;
                            color: #6c6b6b;
                          "
                          >元，或订单数达到</label
                        >
                        <el-input
                          v-model="data.orderSum"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label style="margin-left: 10px; color: #6c6b6b"
                          >次</label
                        >
                      </el-form-item>
                    </el-col>
                  </div>
                  <div v-if="data.labelPattern == 3">
                    <el-col :span="12">
                      <el-form-item>
                        <label style="margin-right: 10px; color: #6c6b6b"
                          >订单金额超过</label
                        >
                        <el-input
                          v-model="data.amountSum"
                          :style="{ width: '15%' }"
                          @keyup.native="keyupEvent($event, input)"
                        >
                        </el-input>
                        <label
                          style="
                            margin-left: 10px;
                            margin-right: 10px;
                            color: #6c6b6b;
                          "
                          >元，距最近一次下单超过</label
                        >
                        <el-input
                          v-model="data.labelDay"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label style="margin-left: 10px; color: #6c6b6b"
                          >天</label
                        >
                      </el-form-item>
                    </el-col>
                  </div>
                  <div v-if="data.labelPattern == 4">
                    <el-col :span="12">
                      <el-form-item>
                        <label style="margin-right: 10px; color: #6c6b6b"
                          >近</label
                        >
                        <el-input
                          v-model="data.labelDay"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label
                          style="
                            margin-left: 10px;
                            margin-right: 10px;
                            color: #6c6b6b;
                          "
                          >天内有订单记录，且历史下单超过</label
                        >
                        <el-input
                          v-model="data.orderSum"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label style="margin-left: 10px; color: #6c6b6b"
                          >次</label
                        >
                      </el-form-item>
                    </el-col>
                  </div>
                  <div v-if="data.labelPattern == 5">
                    <el-col :span="12">
                      <el-form-item>
                        <label style="margin-right: 10px; color: #6c6b6b"
                          >近</label
                        >
                        <el-input
                          v-model="data.labelDay"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label
                          style="
                            margin-left: 10px;
                            margin-right: 10px;
                            color: #6c6b6b;
                          "
                          >天内无订单记录，且历史下单超过</label
                        >
                        <el-input
                          v-model="data.orderSum"
                          :style="{ width: '15%' }"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                        >
                        </el-input>
                        <label
                          style="margin-left: 10px; color: #6c6b6b"
                          oninput="value=value.replace(/^0|[^0-9]/g, '')"
                          >次</label
                        >
                      </el-form-item>
                    </el-col>
                  </div>
                </el-form>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </template>
      <template v-slot:footer>
        <div class="footer">
          <el-button type="" @click="getList" :loading="loading2"
            >刷新</el-button
          >
          <el-button type="primary" @click="submitForm" :loading="loading">
            保存</el-button
          >
        </div>
      </template>
    </CustomContent>
  </div>
</template>

<script>
import { listLabel, updateLabel } from '@/api/system/label'
import MmvTitle from '@/components/MmvUI/MmvTitle.vue'
import CustomContent from '@/components/custom/content.vue'
export default {
  components: {
    MmvTitle,
    CustomContent,
  },
  dicts: ['label_type', 'label_classify', 'label_pattern'],
  name: 'Label',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      loading: false,
      loading2: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签表格数据
      labelList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labelType: null,
        labelClassify: null,
        labelPattern: null,
        orderSum: null,
        amountSum: null,
        modelSum: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    keyupEvent(e, input) {
      e.target.value = e.target.value.replace(/[^\d.]/g, '')
      e.target.value = e.target.value.replace(/\.{2,}/g, '.')
      e.target.value = e.target.value.replace(/^\./g, '0.')
      e.target.value = e.target.value.replace(
        /^\d*\.\d*\./g,
        e.target.value.substring(0, e.target.value.length - 1)
      )
      e.target.value = e.target.value.replace(/^0[^\.]+/g, '0')
      e.target.value = e.target.value.replace(/^(\d+)\.(\d\d).*$/, '$1.$2')
      this.input = e.target.value
      console.log(this.input)
    },
    /** 查询标签列表 */
    getList() {
      this.loading2 = true
      listLabel(this.queryParams).then((response) => {
        this.labelList = response.rows
        this.total = response.total
        this.loading2 = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        labelId: null,
        labelType: null,
        labelClassify: null,
        labelPattern: null,
        orderSum: null,
        amountSum: null,
        modelSum: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.labelId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加标签'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const labelId = row.labelId || this.ids
      getLabel(labelId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改标签'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.loading = true
      this.labelList.forEach((item) => {
        if (item.amountSum != null) {
          item.amountSum = parseFloat(item.amountSum)
        }
      })
      updateLabel(this.labelList).then((response) => {
        this.$modal.msgSuccess('修改成功')
        this.loading = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const labelIds = row.labelId || this.ids
      this.$modal
        .confirm('是否确认删除标签编号为"' + labelIds + '"的数据项？')
        .then(function () {
          return delLabel(labelIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有标签数据项？')
        .then(() => {
          this.exportLoading = true
          return exportLabel(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
<style scoped="scoped">
/deep/ .el-input__inner {
  height: 36px;
  line-height: 36px;
  border-color: rgb(26 132 179) !important;
  color: rgb(26 132 179);
}
</style>
